package com.jetty;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.plus.webapp.EnvConfiguration;
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.FragmentConfiguration;
import org.eclipse.jetty.webapp.MetaInfConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.eclipse.jetty.webapp.WebXmlConfiguration;

public class JettyStart {
	public static void main(String[] args) throws Exception {
		start2();
	}

	/**
	 * 启动jetty 9
	 * @return 
	 * 
	 * @return
	 * @throws Exception
	 */
	public static void start() throws Exception {
		long start = System.currentTimeMillis();
		Server server = new Server();
		// jetty8
		// Connector connector = new SelectChannelConnector();
		// jetty 9
		HttpConfiguration config = new HttpConfiguration();
		ServerConnector connector = new ServerConnector(server,
				new HttpConnectionFactory(config));
		connector.setReuseAddress(true);
		connector.setIdleTimeout(30000);
		// 设置端口
		connector.setPort(8081);
		// 设置host地址
		connector.setHost("127.0.0.1");
		server.setConnectors(new Connector[] { connector });

		String webapp = "target/jetty";
		
		// 设置根路径
		WebAppContext context = new WebAppContext(webapp, "/jetty");
		
		// 启用各类配置
		context.setConfigurations(new Configuration[] {
				new WebXmlConfiguration(),
				new AnnotationConfiguration() ,
				new WebInfConfiguration(), 
				new PlusConfiguration(),
				new MetaInfConfiguration(),
				new FragmentConfiguration(), 
				new EnvConfiguration() 
				});
		// new TagLibConfiguration(),这个在jetty9中失效了
		context.setParentLoaderPriority(true);
		
		context.setDescriptor(webapp + "/WEB-INF/web.xml");
		
		server.setHandler(context);
		server.setStopAtShutdown(true);
		// server.setSendServerVersion(true);

		// 启动服务
		server.start();
		
		System.out.println("Jetty Started ==> "
				+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
						.format(new Date()));
		long end = System.currentTimeMillis();
		System.out.println("启动耗时 ==> " + (end - start) + "ms");
		server.join();
	}
	
	/**
	 * 启动jetty9 完美支持，servlet3.0,和springmvc
	 * 
	 * @throws Exception
	 */
	public static void start2() throws Exception {
		long start = System.currentTimeMillis();
		
		Server server = new Server(8081);
		
		String wardir = "target/jetty";
		
		WebAppContext context = new WebAppContext();
		context.setResourceBase(wardir);
		context.setDescriptor(wardir + "WEB-INF/web.xml");
		
		context.setConfigurations(new Configuration[] {
				new AnnotationConfiguration(), new WebXmlConfiguration(),
				new WebInfConfiguration(), 
				new PlusConfiguration(), new MetaInfConfiguration(),
				new FragmentConfiguration(), new EnvConfiguration() });

		context.setContextPath("/jetty");
		context.setParentLoaderPriority(true);
		server.setHandler(context);
		server.setStopAtShutdown(true);
		server.start();
		// 显示目录下文件信息
		//server.dump(System.err);
		
		System.out.println("Jetty Started ==> "
				+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
						.format(new Date()));
		long end = System.currentTimeMillis();
		System.out.println("启动耗时 ==> " + (end - start) + "ms");
		server.join();
	}
}
